using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Lexalytics;

namespace SalienceEngineDemo
{
    public partial class ctrlGetDocumentDetails : UserControl, DemoControl
    {
        public RichTextBox oTextBox;

        public ctrlGetDocumentDetails(RichTextBox oTextBox)
        {
            InitializeComponent();
            this.oTextBox = oTextBox;
            EnableProcessButton();
        }

        public void processText()
        {
            gridTermFrequency.Rows.Clear();
            gridBiGramFrequency.Rows.Clear();
            gridTriGramFrequency.Rows.Clear();

            try
            {
                SalienceDemo.oSalienceSession.PrepareText(this.oTextBox.Text);
                SalienceDocumentDetails oDetails = SalienceDemo.oSalienceSession.GetDocumentDetails();

                if (oDetails.TermFrequency.Count > 0)
                {
                    //Put the results in the grid
                    int nCount = 0;
                    foreach (SalienceToken oPhrase in oDetails.TermFrequency)
                    {
                        gridTermFrequency.Rows.Add();
                        gridTermFrequency.Rows[nCount].Cells[0].Value = oPhrase.sToken;
                        gridTermFrequency.Rows[nCount].Cells[1].Value = oPhrase.nCount;
                        nCount++;
                    }
                    gridTermFrequency.Sort(gridTermFrequency.Columns[1], ListSortDirection.Descending);
                    gridTermFrequency.AutoResizeRows();
                }

                if (oDetails.BiGramFrequency.Count > 0)
                {
                    //Put the results in the grid
                    int nCount = 0;
                    foreach (SalienceToken oPhrase in oDetails.BiGramFrequency)
                    {
                        gridBiGramFrequency.Rows.Add();
                        gridBiGramFrequency.Rows[nCount].Cells[0].Value = oPhrase.sToken;
                        gridBiGramFrequency.Rows[nCount].Cells[1].Value = oPhrase.nCount;
                        nCount++;
                    }
                    gridBiGramFrequency.Sort(gridBiGramFrequency.Columns[1], ListSortDirection.Descending);
                    gridBiGramFrequency.AutoResizeRows();
                }

                if (oDetails.TriGramFrequency.Count > 0)
                {
                    //Put the results in the grid
                    int nCount = 0;
                    foreach (SalienceToken oPhrase in oDetails.TriGramFrequency)
                    {
                        gridTriGramFrequency.Rows.Add();
                        gridTriGramFrequency.Rows[nCount].Cells[0].Value = oPhrase.sToken;
                        gridTriGramFrequency.Rows[nCount].Cells[1].Value = oPhrase.nCount;
                        nCount++;
                    }
                    gridTriGramFrequency.Sort(gridTriGramFrequency.Columns[1], ListSortDirection.Descending);
                    gridTriGramFrequency.AutoResizeRows();
                }
            }
            catch (SalienceException e)
            {
                MessageBox.Show(e.Message, "Salience Demo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnProcess_Click(object sender, EventArgs e)
        {
            processText();
        }

        private void getDocumentDetails_VisibleChanged(object sender, EventArgs e)
        {
            gridTermFrequency.Rows.Clear();
            gridBiGramFrequency.Rows.Clear();
            gridTriGramFrequency.Rows.Clear();
        }

        #region DemoControl interface methods
        public void btnOptions_Click(object sender, EventArgs e)
        {
            ((SalienceDemo)this.Parent).StartOptionsDialog((int)Dialogs.Options.OptionSections.NOTHING);
            EnableProcessButton();
        }

        public void SetFontSize()
        {
            this.gridBiGramFrequency.DefaultCellStyle.Font = new Font(this.oTextBox.Font.FontFamily, Properties.Settings.Default.fFontSize);
            this.gridTermFrequency.DefaultCellStyle.Font = new Font(this.oTextBox.Font.FontFamily, Properties.Settings.Default.fFontSize);
            this.gridTriGramFrequency.DefaultCellStyle.Font = new Font(this.oTextBox.Font.FontFamily, Properties.Settings.Default.fFontSize);
            this.gridBiGramFrequency.AutoResizeRows();
            this.gridTermFrequency.AutoResizeRows();
            this.gridTriGramFrequency.AutoResizeRows();
        }

        public void EnableProcessButton()
        {
            // Process button enabled by existence of text to process
            this.btnProcess.Enabled = (oTextBox.Text.Trim().Length > 0);
        }

        #endregion

        private void groupBox1_SizeChanged(object sender, EventArgs e)
        {
            int nMarginWidth = 6;
            int nSpacer = 42;

            int nGridWidth = (groupBox1.Width - ((nMarginWidth * 2) + (nSpacer * 2))) / 3;
            gridTermFrequency.Width = gridBiGramFrequency.Width = gridTriGramFrequency.Width = nGridWidth;
            gridTermFrequency.Left = nMarginWidth;
            gridBiGramFrequency.Left = gridTermFrequency.Left + nGridWidth + nSpacer;
            gridTriGramFrequency.Left = gridBiGramFrequency.Left + nGridWidth + nSpacer;
        }       
    }
}
